﻿StaffList = Class.create();

StaffList.prototype = Object.extend(new Common(), {
    initialize: function () {
        this.initCommonProperty();
    },
    init: function () {
        //添加权限
        Event.observe($("position_btnAdd"), 'click', this.onClick_addPosition.bind(this));

        //修改权限
        Event.observe($("position_btnUpdate"), 'click', this.onClick_updatePosition.bind(this));

        //删除权限
        Event.observe($("position_btnDelete"), 'click', this.onClick_deletePosition.bind(this));

        //查找Staff信息
        Event.observe($("staff_btnSearch"), 'click', this.onClick_search.bind(this));

        //添加Staff
        Event.observe($("staff_btnAdd"), 'click', this.onClick_addStaff.bind(this));

        //删除Staff
        Event.observe($("staff_btnDelete"), 'click', this.onClick_deleteStaff.bind(this));

        //清空staff查询条件
        Event.observe($("staff_btnClear"), 'click', this.onClick_Clear.bind(this));

        //实例化treeView对象
        this.cmnTreeViewIns = new CmnTreeView();

        this.cmnSplitterIns = new CmnSplitter();

        this.initSplitter();

        this.position_json;

        //获取职位List
        this.getPositionList();

        //定义head的显示
        this.colList = new Array();

        //定义tableIns实例
        this.tableIns = new Table();

        //显示多选框
        this.tableIns.useCheckOne = true;

        //主键Column
        this.tableIns.pkColumnName = "staff_id";

        //显示条数
        this.tableIns.setPageSize(13);

        //定义head的显示
        this.initTableColList();

        this.position_id = "";
    },
    //定义head的显示
    initTableColList: function () {
        this.colList.push(new ColInfo("帐号", "staff_code", "10%", SORT_YES));
        this.colList.push(new ColInfo("名字", "staff_name", "10%", SORT_YES));
        this.colList.push(new ColInfo("电话/手机", "staff_phone", "10%"));
        this.colList.push(new ColInfo("邮件", "staff_email", "25%"));
        this.colList.push(new ColInfo("地址", "staff_address", "25%"));
        this.colList.push(new ColInfo("创建者", "create_user", "10%"));
        this.colList.push(new ColInfo("修改者", "update_user", "10%"));
    },
    //实现可以自定义拖动div
    initSplitter: function () {
        //自定义拖动div的高度
        this.cmnSplitterIns.height = 670;

        //定义拖动div上部分高度
        this.cmnSplitterIns.topPanel.initHeight = 50;

        //定义左侧宽度
        this.cmnSplitterIns.leftPanel.initWidth = 200;

        //显示上部分DIV
        this.cmnSplitterIns.hasTopPanel = true;

        //自定义拖动div的init处理
        this.cmnSplitterIns.init("MySplitter", "position_div", "staffListByPositionId", "btn_div");

        //自定义拖动div的实现
        this.cmnSplitterIns.create();
    },

    //获取职位List
    getPositionList: function () {
        CmnMessage.showProcessing();

        //发送Ajax
        this.sendAjaxByModel("/Master/GetPositionList", null, this.getPositionListCallback.bind(this));
    },
    //获取职位List的callback
    getPositionListCallback: function (response) {
        this.position_json = this.jsonParse(response);

        //treeView中要显示的列
        this.cmnTreeViewIns.displayColumnName = "position_name";

        //treeView中显示列的id
        this.cmnTreeViewIns.valueColumnName = "position_id";

        //该列处于那种deep（及层次）
        this.cmnTreeViewIns.deepColumnName = "deep";

        //数据库中对应的主键id
        this.cmnTreeViewIns.pkColumnName = "position_id";

        //父子关系列
        this.cmnTreeViewIns.parentColumnName = "parent_id";

        //调用treeView方法的init
        this.cmnTreeViewIns.init("positionTreeView", this.position_json);

        //实现treeView
        this.cmnTreeViewIns.create();

        //选中treeView中的displayColumnName时触发事件
        this.cmnTreeViewIns.rowSelectedCallback = this.getRowSelectedStaffList.bind(this);

        CmnMessage.hide();

    },
    //选中treeView中的displayColumnName时触发事件
    getRowSelectedStaffList: function (position_id) {

        this.cmnTreeViewIns.setSelectingRow(position_id);

        this.position_id = position_id;

        var searchModel = this.getSearchModel();

        //把选中的treeView节点的position_id给个后台where
        searchModel.position_id = position_id;

        //生成where和分页信息
        var data = this.getListCondition(searchModel, this.tableIns.pager);

        CmnMessage.showProcessing();

        //发送Ajax
        this.sendAjaxBySearchCondition("/Master/GetStaffListByPositionId", data, this.getStaffListCallback.bind(this));

    },
    getStaffListCallback: function (response) {
        var json = this.jsonParse(response);

        //话table的init事件处理
        this.tableIns.init("staffListByPositionId", json, this.colList, this.cellCallback.bind(this));

        //创建table
        this.tableIns.create();

        //绑定点击页码触发事件
        this.tableIns.changePageCallback = this.getRowSelectedStaffList.bind(this);

        //绑定点击title时触发排序事件
        this.tableIns.clientSortCallback = this.getRowSelectedStaffList.bind(this);

        CmnMessage.hide();
    },
    //给单元格做处理事件
    cellCallback: function (rowData, colName, rowNo) {
        if (colName == "staff_name") {
            return "<a href='#' onclick='goStaffInput(" + rowData.staff_id + ");'>" + rowData.staff_name + "</a>"
        }

        return rowData[colName];
    },
    //添加StaffInfo
    onClick_addStaff: function () {
        this.staffInputIns.show(this.getRowSelectedStaffList.bind(this));
    },
    //添加事件处理
    onClick_addPosition: function () {
        //判断是否选中节点
        if (this.cmnTreeViewIns.selectedRowData == null) {
            CmnMessage.showMessage("请选择一个父节点", MSG_TYPE_INFO);
            return;
        }
        //调用input画面
        this.positionInputIns.show(null, this.getPositionList.bind(this), this.cmnTreeViewIns.selectedRowData);
    },
    //删除事件处理
    onClick_deletePosition: function () {

        if (this.cmnTreeViewIns.selectedRowData == null) {
            CmnMessage.showMessage("请选择一个职位", MSG_TYPE_INFO);
            return;
        }
        else {
            //删除操作
            if (this.cmnTreeViewIns.selectedRowData.has_child > 0) {
                CmnMessage.showMessage("此节点拥有子类是否要全部删除？", MSG_TYPE_CONFIRM, this.deleteData.bind(this));
            }
            else {
                CmnMessage.showMessage("是否删除？", MSG_TYPE_CONFIRM, this.deleteData.bind(this));
            }

        }
    },
    //删除处理
    deleteData: function () {
        var data = {};
        data.position_id = this.cmnTreeViewIns.selectedRowData[this.cmnTreeViewIns.pkColumnName];
        this.sendAjaxByModel("/Master/DeletePositionInfo", data, this.onClickDeleteCallback.bind(this));
    },
    //删除CallBack
    onClickDeleteCallback: function (response) {
        var json = this.jsonParse(response);
        if (json.result > 0) {
            CmnMessage.showMessage("删除成功.", MSG_TYPE_INFO, this.messageCallBack.bind(this));
        }
        else {
            CmnMessage.showMessage("删除失败！", MSG_TYPE_ERROR);
        }
    },
    //画面弹出Message框点击确定或取消后处理
    messageCallBack: function () {
        CmnDialog.hide($("addPositionInputFrame"));

        //获取职位List
        this.getPositionList();
    },
    //修改权限
    onClick_updatePosition: function () {
        //判断是否选中节点
        if (this.cmnTreeViewIns.selectedRowData == null) {
            CmnMessage.showMessage("请选择一个职位", MSG_TYPE_INFO);
            return;
        }

        //获取要修改的position_id
        var position_id = this.cmnTreeViewIns.selectedRowData[this.cmnTreeViewIns.pkColumnName];

        //调用input画面
        this.positionInputIns.show(position_id, this.getPositionList.bind(this), null);
    },
    //删除処理
    onClick_deleteStaff: function () {
        //是否选中行
        if (this.tableIns.checkedDataList.length <= 0) {
            CmnMessage.showMessage("没有选中任何行!", MSG_TYPE_INFO);
        }
        else {
            CmnMessage.showMessage("是否删除此信息？", MSG_TYPE_CONFIRM, this.deleteStaffData.bind(this));
        }
    },
    //删除Staff处理
    deleteStaffData: function () {
        this.sendAjaxByList("/Master/DeleteStaffInfo", this.tableIns.checkedPKIDList, this.onClickDeleteStaffCallback.bind(this));
    },
    //删除Staff CallBack
    onClickDeleteStaffCallback: function (response) {
        var json = this.jsonParse(response);
        if (json.result > 0) {
            CmnMessage.showMessage(json.message, MSG_TYPE_INFO, this.messageStaffCallBack.bind(this));
        }
        else {
            CmnMessage.showMessage(json.message, MSG_TYPE_ERROR);
        }
    },
    messageStaffCallBack: function () {
        //获取当前position_id对应的StaffList
        this.getRowSelectedStaffList(this.position_id);
    },
    //查找员工信息
    onClick_search: function () {
        this.tableIns.pager.pageIndex = 1;
        this.getRowSelectedStaffList(this.position_id);
    },
    //清空查询条件
    onClick_Clear: function () {
        this.clearForm();
    }
});

//点击姓名show出修改控件
function goStaffInput(staff_id) {
    staffListIns.staffInputIns.show(staffListIns.getRowSelectedStaffList.bind(staffListIns), staff_id);
}